#include "updatetimedialog.h"
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>

UpdateTimeDialog::UpdateTimeDialog(double updateTime, QWidget *parent)
    : QDialog (parent)
{
    // widget
    setWindowTitle("UpdatePeriod");
    setAttribute(Qt::WA_DeleteOnClose, true);
    // controls
    QLabel *lbTime = new QLabel();
    lbTime->setText("updatePeriod");
    QLineEdit *leTime = new QLineEdit();
    leTime->setText(QString::number(updateTime));
    QPushButton *pbConfirm = new QPushButton();
    pbConfirm->setText("Confirm");
    QPushButton *pbCancel = new QPushButton();
    pbCancel->setText("Cancel");
    // layout
    QHBoxLayout *hblTime = new QHBoxLayout();
    hblTime->addWidget(lbTime, 1);
    hblTime->addWidget(leTime, 1);
    QHBoxLayout *hblButton = new QHBoxLayout();
    hblButton->addWidget(pbConfirm, 1);
    hblButton->addWidget(pbCancel, 1);
    QVBoxLayout *vbl = new QVBoxLayout();
    vbl->addLayout(hblTime, 1);
    vbl->addLayout(hblButton, 1);
    setLayout(vbl);

    connect(pbConfirm, &QPushButton::clicked, [=](){
        double time = leTime->text().toDouble();

        emit setUpdatePeriod(time);

        close();
    });
    connect(pbCancel, &QPushButton::clicked, this, &QWidget::close);
}
